﻿using System.Text;
using Dapper;
using Microsoft.Data.SqlClient;
using Production.CommonLib.Models;
using Swimj.CommonLib.Base;
using Swimj.DataCore;

namespace Production.ApplicationLib.Queries
{
    public class ProductionResourceQueries : IProductionResourceQueries
    {
        private readonly string? _defaultConnStr;

        public ProductionResourceQueries(string? defaultConnStr)
        {
            _defaultConnStr = defaultConnStr;
        }

        public async Task<PaginatedResult<ProductionResourceInfoResponseDto>?> QueryProductionResourcesByPage(ProductionResourceSearchRequestDto model)
        {
            IEnumerable<ProductionResourceInfoResponseDto>? data;
            int totalCount;
            using (var db = new SqlConnection(_defaultConnStr))
            {
                var query = new Query(db);

                StringBuilder sqlBuilder = new StringBuilder(@"
select Id,
       Code,
       Name,
       ProcessType,
       ProductionType,
       Quality,
       Sort,
       Description,
       DataState,
       CreatedBy,
       CreatedOn,
       LastModifiedBy,
       LastModifiedOn,
       DeletedBy,
       DeletedOn
from MES_PRODUCTIONRESOURCE where 1 = 1 ");

                query.BuildPageQueries(model.Page, model.Limit, sqlBuilder.ToString());

                data = await db.QueryAsync<ProductionResourceInfoResponseDto>(query.SqlPage);

                totalCount = await db.ExecuteScalarAsync<int>(query.SqCount);
            }

            return await PaginatedResult<ProductionResourceInfoResponseDto>.SuccessAsync(data.ToList(), totalCount, model.Page,
                model.Limit);
        }

        public Task<Result<IList<ProductionResourceInfoResponseDto>>?> QueryProductionResources(ProductionResourceSearchRequestDto model)
        {
            throw new NotImplementedException();
        }

        public Task<Result<ProductionResourceInfoResponseDto>?> GetProductionResource(int id)
        {
            throw new NotImplementedException();
        }
    }
}